package com.zhiban.project.controller;
import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang.StringUtils;

import com.opensymphony.util.FileUtils;

import dinamica.Recordset;
import dinamica.audit.AuditableTransaction;

public class TemplateController extends AuditableTransaction
{

	public TemplateController()
	{
	}

	public int service(Recordset inputParams)
		throws Throwable
	{
		HttpServletRequest request = getRequest();
		String content = inputParams.getString("content");
		String old_filename = inputParams.getString("path");
		String datestr = DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
		String filename = (new StringBuilder(String.valueOf(datestr))).append(".html").toString();
		String path = request.getRealPath("/template");
		if (StringUtils.isNotBlank(old_filename))
		{
			File oldFile = new File(path, old_filename);
			oldFile.delete();
		}
		File templateFile = new File(path, filename);
		File template1File = new File(path, "template.html");
		String cstr = FileUtils.readFile(template1File);
		cstr = cstr.replace("${content}", content);
		FileUtils.write(templateFile, cstr);
		inputParams.setValue("path", filename);
		inputParams.setValue("content", "c");
		String insert = getSQL(getResource("insert.sql"), inputParams);
		getDb().exec(insert);
		int rc = 0;
		super.service(inputParams);
		
		String sqlAudit = getConfig().getConfigValue("//audit/recordset");//添加日志
		if (sqlAudit != null && !sqlAudit.equals(""))
		{
			Recordset rs = getDb().get(getSQL(getResource(sqlAudit), inputParams));
			publish(sqlAudit, rs);
		}
		return rc;
	}
}